package servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;

import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.FinderException;
import javax.ejb.RemoveException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import offer.Offer;
import offer.OfferHome;
import recruiter.Recruiter;
import recruiter.RecruiterHome;
import recruiterSession.RecruiterSession;
import student.Student;
import student.StudentHome;

import company.Company;
import company.CompanyHome;
import compsup.CompanySup;
import compsup.CompanySupHome;

import descriptor.OfferDescriptor;
import descriptor.StudentDescriptor;


public class RecruiterServlet extends HttpServlet {
	protected void doGet(HttpServletRequest req, HttpServletResponse res)
	throws IOException, ServletException
	{
		doPostAndGet(req,res);
	}

	protected void doPost(HttpServletRequest req, HttpServletResponse res)
	throws IOException, ServletException
	{
		doPostAndGet(req,res);
	}

	protected void doPostAndGet(HttpServletRequest req ,HttpServletResponse res)
	throws IOException, ServletException
	{
		Context initialContext = null;
		try {
			initialContext = new InitialContext() ;
		} catch (NamingException e) {
			e.printStackTrace();
		}
		HttpSession session = req.getSession(true);
		RecruiterSession recruiter = (RecruiterSession)session.getAttribute("recruiter");
		String page = req.getParameter("page") ;
		RequestDispatcher dispatcher = null ;

		if(recruiter == null){
			dispatcher = req.getRequestDispatcher("index.jsp") ;
			dispatcher.forward(req, res) ;
			return;
		}
		req.setAttribute("recruiter", recruiter.getDescriptor());
		if(page.equalsIgnoreCase("index")){
			session.removeAttribute("recruiter");
			dispatcher = req.getRequestDispatcher("index.jsp");
		}if(page.equalsIgnoreCase("recruiterView")){
			req.setAttribute("studentList", recruiter.getInterestedStudent());
			req.setAttribute("degreeList", recruiter.getDegreeDescriptor());
			dispatcher = req.getRequestDispatcher("recruiter/recruiterView.jsp");
		}if(page.equalsIgnoreCase("recruiterOfferView")){
			req.setAttribute("offerList",recruiter.getOfferDescriptor());
			dispatcher = req.getRequestDispatcher("recruiter/recruiterOfferView.jsp");
		}if(page.equalsIgnoreCase("recruiterAddPublicOffer")){
			req.setAttribute("compSupList",recruiter.getCompanySupForTheCompanyDescriptor());
			dispatcher = req.getRequestDispatcher("recruiter/recruiterAddPublicOffer.jsp");
		}if(page.equalsIgnoreCase("addPublicOfferDone")){
			InitialContext context = null;
			OfferHome offerHome = null;
			CompanyHome companyHome = null;
			RecruiterHome recruiterHome = null;
			Company company = null;
			Recruiter recruiterBean = null;
			CompanySup companySup = null;
			String dateOfStart = req.getParameter("calendar1");
			String dateOfEnd = req.getParameter("calendar2");
			String name = req.getParameter("title");
			String description = req.getParameter("description");
			int companySupId = Integer.parseInt(req.getParameter("companySup"));

			try {
				context = new InitialContext();
				offerHome = (OfferHome)context.lookup("MyOffer_L");
				companyHome = (CompanyHome)context.lookup("MyCompany_L");
				recruiterHome = (RecruiterHome)context.lookup("MyRecruiter_L");
				
			} catch (NamingException e) {
				e.printStackTrace();
			}
			try {
				recruiterBean = recruiterHome.findByPrimaryKey(recruiter.getId());
				company = companyHome.findByPrimaryKey(recruiterBean.getCompany().getId());
				companySup = getCompanySup(companySupId);
				offerHome.create(name, description, 1, dateOfStart, dateOfEnd, company, recruiterBean,null,companySup);
			} catch (CreateException e) {
				e.printStackTrace();
			} catch (FinderException e) {
				e.printStackTrace();
			}
			req.setAttribute("offerList",recruiter.getOfferDescriptor());
			dispatcher = req.getRequestDispatcher("recruiter/recruiterOfferView.jsp");
		}if(page.equalsIgnoreCase("recruiterViewStudent")){
			InitialContext context = null;
			StudentHome studenthome = null;
			Student student = null;
			try {
				context = new InitialContext();
				studenthome = (StudentHome)context.lookup("MyStudent_L");
			} catch (NamingException e) {
				e.printStackTrace();
			}

			int studentId = Integer.parseInt(req.getParameter("studentId"));
			try {
				student = studenthome.findByPrimaryKey(studentId);
			} catch (FinderException e) {
				e.printStackTrace();
			}
			req.setAttribute("student", student.getDescriptor());
			dispatcher = req.getRequestDispatcher("recruiter/recruiterViewStudent.jsp");

		}if(page.equalsIgnoreCase("recuiterAddPersonalInterview")){
			int studentId = Integer.parseInt(req.getParameter("studentId"));
			Student student = getStudent(studentId);
			req.setAttribute("student", student.getDescriptor());
			dispatcher = req.getRequestDispatcher("recruiter/recuiterAddPersonalInterview.jsp");
		}if(page.equalsIgnoreCase("recuiterAddPersonalOffer")){
			int studentId = Integer.parseInt(req.getParameter("studentId"));
			Student student = getStudent(studentId);
			req.setAttribute("compSupList",recruiter.getCompanySupForTheCompanyDescriptor());
			req.setAttribute("student", student.getDescriptor());
			dispatcher = req.getRequestDispatcher("recruiter/recuiterAddPersonalOffer.jsp");
		}if(page.equalsIgnoreCase("recruiterAddPersonnalOfferDone")){
			InitialContext context = null;
			OfferHome offerHome = null;
			CompanyHome companyHome = null;
			RecruiterHome recruiterHome = null;
			Company company = null;
			Recruiter recruiterBean = null;
			String dateOfStart = req.getParameter("calendar1");
			String dateOfEnd = req.getParameter("calendar2");
			String name = req.getParameter("title");
			String description = req.getParameter("description");
			int companySupId = Integer.parseInt(req.getParameter("companySup"));
			int studentId = Integer.parseInt(req.getParameter("studentId"));
			Student student = getStudent(studentId);

			
			
			//Interview side
			String message = "You have been propose an interview with "+recruiter.getFirstname() +" "+recruiter.getSurname()+"\n";
			message += recruiter.getFirstname() +" "+recruiter.getSurname()+"\n";
			message += recruiter.getFirstname() +" "+recruiter.getSurname()+" hope to see you on"; 

			String date = req.getParameter("interviewDay");
			String hour = req.getParameter("hour");
			String minute = req.getParameter("minute");
			String address = req.getParameter("address");
			
			message +=" "+date+" at "+hour+"h"+minute+" at "+address;

			/**
			try{
				Socket socket = new Socket("smtp.student.cs.ucc.ie", 25);
				// Create an output stream for sending message
				PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

				// send mail using SMTP protocol
				out.println("MAIL FROM: " + recruiter.getEmail());
				out.println("RCPT TO: " + student.getMail());
				out.println("DATA\n");  // Skip line after DATA
				out.println(message);
				out.println("."); 
			}catch(Exception ex){
				ex.printStackTrace();
				
			}
			**/


			try {
				context = new InitialContext();
				offerHome = (OfferHome)context.lookup("MyOffer_L");
				companyHome = (CompanyHome)context.lookup("MyCompany_L");
				recruiterHome = (RecruiterHome)context.lookup("MyRecruiter_L");

			} catch (NamingException e) {
				e.printStackTrace();
			}
			try {
				recruiterBean = recruiterHome.findByPrimaryKey(recruiter.getId());
				company = companyHome.findByPrimaryKey(recruiterBean.getCompany().getId());
				CompanySup companySup = getCompanySup(companySupId);
				Offer offer = offerHome.create(name, description, 0, dateOfStart, dateOfEnd, company, recruiterBean, student,companySup);
				offer.setAddressOfInterview(address);
				offer.setDateOfInterview(date);
				offer.setMinuteOfInterview(minute);
				offer.setHourOfInterview(hour);
				offer.setStatus(4);
				
			} catch (CreateException e) {
				e.printStackTrace();
			} catch (FinderException e) {
				e.printStackTrace();
			}
			req.setAttribute("offerList",recruiter.getOfferDescriptor());
			dispatcher = req.getRequestDispatcher("recruiter/recruiterOfferView.jsp");
		}if(page.equalsIgnoreCase("recruiterAddPersonnalInterviewDone")){
			String message = "You have been propose an interview with "+recruiter.getFirstname() +" "+recruiter.getSurname()+"\n";
			message += recruiter.getFirstname() +" "+recruiter.getSurname();
			message += " and hope to see you on"; 
			int studentId = Integer.parseInt(req.getParameter("studentId"));
			String date = req.getParameter("interviewDay");
			String hour = req.getParameter("hour");
			String minute = req.getParameter("minute");
			String address = req.getParameter("address");
			
			message +=" "+date+" at "+hour+"h"+minute+" at "+address;
			Student student = getStudent(studentId);
			
			/**
			try{
				Socket socket = new Socket("smtp.student.cs.ucc.ie", 25);
				// Create an output stream for sending message
				PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

				// send mail using SMTP protocol
				out.println("MAIL FROM: " + recruiter.getEmail());
				out.println("RCPT TO: " + student.getMail());
				out.println("DATA\n");  // Skip line after DATA
				out.println(message);
				out.println("."); 
			}catch(Exception ex){
				ex.printStackTrace();
				
			}
			**/
			req.setAttribute("offerList",recruiter.getOfferDescriptor());
			dispatcher = req.getRequestDispatcher("recruiter/recruiterOfferView.jsp");
		}if(page.equalsIgnoreCase("recruiterViewAnOffer")){
			int offerId = Integer.parseInt(req.getParameter("offerId"));
			Offer offer = getOffer(offerId);
			OfferDescriptor offerDesc = offer.getDescriptor();
			StudentDescriptor student = null;
			if(offer.getType()==0){
				student = getStudent(offer.getStudent().getId()).getDescriptor();
			}

			req.setAttribute("compSupList",recruiter.getCompanySupForTheCompanyDescriptor());
			req.setAttribute("student", student);
			req.setAttribute("offer", offerDesc);
			req.setAttribute("result", "");
			dispatcher = req.getRequestDispatcher("recruiter/recruiterViewAnOffer.jsp");

		}if(page.equalsIgnoreCase("UpdateOfferDone")){
			String dateOfStart = req.getParameter("calendar1");
			String dateOfEnd = req.getParameter("calendar2");
			String name = req.getParameter("title");
			String description = req.getParameter("description");
			int offerId = Integer.parseInt(req.getParameter("offerId"));
			int companySupId = Integer.parseInt(req.getParameter("companySup"));
			Offer offer = getOffer(offerId);
			//Student studentBean = getStudent(offer.getStudent().getId());

			//Interview side
			String message = "You have been propose an interview with "+recruiter.getFirstname() +" "+recruiter.getSurname()+"\n";
			message += recruiter.getFirstname() +" "+recruiter.getSurname()+"\n";
			message += recruiter.getFirstname() +" "+recruiter.getSurname()+" hope to see you on";

			String date = req.getParameter("dateOfInterview");
			String hour = req.getParameter("hour");
			String minute = req.getParameter("minute");
			String address = req.getParameter("address");
			
			message +=" "+date+" at "+hour+"h"+minute+" at "+address;

			
			/**
			Socket socket = new Socket("smtp.student.cs.ucc.ie", 25);
			// Create an output stream for sending message
			PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

			// send mail using SMTP protocol
			out.println("MAIL FROM: " + recruiter.getEmail());
			out.println("RCPT TO: " + studentBean.getMail());
			out.println("DATA\n");  // Skip line after DATA
			out.println(message);
			out.println("."); 
			**/
			
			offer.setName(name);
			offer.setDateOfEnd(dateOfEnd);
			offer.setDateOfStart(dateOfStart);
			offer.setDescription(description);
			offer.setCompanySup(getCompanySup(companySupId));
			offer.setAddressOfInterview(address);
			offer.setDateOfInterview(date);
			offer.setMinuteOfInterview(minute);
			offer.setHourOfInterview(hour);
			
			
			StudentDescriptor student = null;
			if(offer.getType()==0){
				student = getStudent(offer.getStudent().getId()).getDescriptor();
			}
			req.setAttribute("compSupList",recruiter.getCompanySupForTheCompanyDescriptor());
			req.setAttribute("student", student);
			req.setAttribute("offer", offer.getDescriptor());
			req.setAttribute("result", "Update Done!");
			dispatcher = req.getRequestDispatcher("recruiter/recruiterViewAnOffer.jsp");



		}if(page.equalsIgnoreCase("recruiterRemoveOffer")){
			int offerId  = Integer.parseInt(req.getParameter("offerId"));
			Offer offer = getOffer(offerId);
			OfferDescriptor desc = offer.getDescriptor();
			req.setAttribute("offer", desc);
			dispatcher = req.getRequestDispatcher("recruiter/recruiterRemoveOffer.jsp");

		}if(page.equalsIgnoreCase("recruiterRemoveOfferDone")){
			int offerId  = Integer.parseInt(req.getParameter("offerId"));
			Offer offer = getOffer(offerId);
			try {
				offer.remove();
			} catch (EJBException e) {
				e.printStackTrace();
			} catch (RemoveException e) {
				e.printStackTrace();
			}

			req.setAttribute("offerList",recruiter.getOfferDescriptor());
			dispatcher = req.getRequestDispatcher("recruiter/recruiterOfferView.jsp");

		}else if(page.equalsIgnoreCase("recruiterConfirmeOffer")){
			int offerId = Integer.parseInt(req.getParameter("offerId"));
			Offer offer = getOffer(offerId);
			offer.setStatus(6);
			req.setAttribute("offerList",recruiter.getOfferDescriptor());
			dispatcher = req.getRequestDispatcher("recruiter/recruiterOfferView.jsp");
		}else if(page.equalsIgnoreCase("recruiterRejectOffer")){
			int offerId = Integer.parseInt(req.getParameter("offerId"));
			Offer offer = getOffer(offerId);
			offer.setStatus(7);
			req.setAttribute("offerList",recruiter.getOfferDescriptor());
			dispatcher = req.getRequestDispatcher("recruiter/recruiterOfferView.jsp");
		}

		if (dispatcher != null) dispatcher.forward(req, res) ;
	}


	public Student getStudent(int studentId){
		InitialContext context = null;
		StudentHome studenthome = null;
		Student student = null;
		try {
			context = new InitialContext();
			studenthome = (StudentHome)context.lookup("MyStudent_L");
		} catch (NamingException e) {
			e.printStackTrace();
		}

		try {
			student = studenthome.findByPrimaryKey(studentId);
		} catch (FinderException e) {
			e.printStackTrace();
		}
		return student;
	}

	public Offer getOffer(int offerId){
		InitialContext context = null;
		OfferHome offerhome = null;
		Offer offer = null;
		try {
			context = new InitialContext();
			offerhome = (OfferHome)context.lookup("MyOffer_L");
		} catch (NamingException e) {
			e.printStackTrace();
		}

		try {
			offer = offerhome.findByPrimaryKey(offerId);
		} catch (FinderException e) {
			e.printStackTrace();
		}
		return offer;
	}
	
	public CompanySup getCompanySup(int companySupId){
		InitialContext context = null;
		CompanySupHome companySuphome = null;
		CompanySup companySup = null;
		try {
			context = new InitialContext();
			companySuphome = (CompanySupHome)context.lookup("MyCompanySup_L");
		} catch (NamingException e) {
			e.printStackTrace();
		}

		try {
			companySup = companySuphome.findByPrimaryKey(companySupId);
		} catch (FinderException e) {
			e.printStackTrace();
		}
		return companySup;
	}
}
